//
// Created by A4-28 on 2020/10/12.
//

#include "camera3DController.h"

namespace WF {

    Camera3DController::Camera3DController():camera(new PerspectiveCamera()) {}
    void Camera3DController::Move(GLFWwindow *w) {
        if (glfwGetKey(w, GLFW_KEY_A) == GLFW_PRESS) {
            camera->Position -= camera->Right * moveSpeed;
        } else if (glfwGetKey(w, GLFW_KEY_D) == GLFW_PRESS) {
            camera->Position += camera->Right * moveSpeed;
        }
        if (glfwGetKey(w, GLFW_KEY_W) == GLFW_PRESS) {
            camera->Position += camera->Forward * moveSpeed;
        } else if (glfwGetKey(w, GLFW_KEY_S) == GLFW_PRESS) {
            camera->Position -= camera->Forward * moveSpeed;
        }
        if (glfwGetKey(w, GLFW_KEY_Q) == GLFW_PRESS) {
            camera->Position += camera->Up * moveSpeed;
        } else if (glfwGetKey(w, GLFW_KEY_E) == GLFW_PRESS) {
            camera->Position -= camera->Up * moveSpeed;
        }
    }

    void Camera3DController::Scroll( float x, float y) {
        if (camera->FOV >= 1.0f && camera->FOV <= 90.0f)
            camera->FOV += y;
        if (camera->FOV <= 1.0f)
            camera->FOV = 1.0f;
        if (camera->FOV >= 90.0f)
            camera->FOV = 90.0f;
    }

    void Camera3DController::ViewRotate(float x, float y) {
        if (isFirst) {
            lastX = x;
            lastY = y;
            isFirst = false;
            return;
        }
        float deltax, deltay;
        deltax = x - lastX;
        deltay = y - lastY;
        lastX = x;
        lastY = y;
        camera->Pitch -= deltay * rotateSpeed;
        camera->Yaw -= deltax * rotateSpeed;
        if (camera->Pitch > 89.0f)
            camera->Pitch = 89.0f;
        if (camera->Pitch < -89.0f)
            camera->Pitch = -89.0f;
        camera->Update();
    }

}